package viz.view.mediators
{
	
	import flare.vis.data.Data;
	import flare.vis.data.EdgeSprite;
	import flare.vis.data.NodeSprite;
	import flare.vis.events.SelectionEvent;
	import flare.vis.events.VisualizationEvent;
	
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	import viz.view.components.FlareViz;

	public class FlareVizMediator extends Mediator
	{
		public static const NAME:String = "FlareVizMediator"
		
		// incoming notes
		public static const FLARE_DATA_UPDATED:String = "flareViz/dataUpdated";
		public static const FLARE_DATA_FLUSHED:String = "flareViz/dataFlashed";
		public static const FLARE_DATA_KEY_UP:String = "flareViz/keyUp";
		public static const FLARE_DATA_KEY_DOWN:String = "flareViz/keyDown";
		
		public static const NODE_ROLLOVER:String = "flareViz/node/rollover";
		public static const NODE_KEY_ROLLOVER:String = "flareViz/node/keyRollover";
		public static const NODE_ROLLOUT:String = "flareViz/node/rollout";
		public static const NODE_KEY_ROLLOUT:String = "flareViz/node/keyRollout";
		public static const NODE_CLICK:String = "flareViz/node/click";
		
		public static const EDGE_ROLLOVER:String = "flareViz/edge/rollover";		
		public static const EDGE_ROLLOUT:String = "flareViz/edge/rollout";
		public static const EDGE_CLICK:String = "flareViz/edge/click";
		
		public static const CONTROL_KEY_UP:String = "flareViz/controlKey/up";
		public static const CONTROL_KEY_DOWN:String = "flareViz/controlKey/down";

		public function FlareVizMediator(viewComponent:Object=null){
			super(NAME, viewComponent);
		}	
		
		override public function onRegister():void{
			view.addEventListener(SelectionEvent.DESELECT, onDeselect);
			view.addEventListener(SelectionEvent.SELECT, onSelect);	
		}	
		
		override public function listNotificationInterests():Array{
			return [	FLARE_DATA_UPDATED,
						FLARE_DATA_FLUSHED,
						FLARE_DATA_KEY_DOWN,
						FLARE_DATA_KEY_UP
															]
		}
		
		override public function handleNotification(note:INotification):void{
			
			switch( note.getName() ){
				
				case FLARE_DATA_UPDATED:
					view.dataProvider = Data( note.getBody() );
					break;
					
				case FLARE_DATA_FLUSHED:
					view.flush();
					break;
					
				case FLARE_DATA_KEY_DOWN:
					onKeyDown();
					break;
					
				case FLARE_DATA_KEY_UP:
					onKeyUp();
					break;

			}
		}
		
		private function onDeselect( event:SelectionEvent ):void{
			switch(true){
				case (event.node != null && event.ctrlKey ):
					sendNodeKeyDeselectNote(event.node);
					break;
					
				case (event.node != null ):
					sendNodeDeselectNote(event.node);
					break;
					
				case (event.edge != null ):
					sendEdgeDeselectNote(event.edge);
					break;
					
			}
		}

		
		private function onSelect( event:SelectionEvent ):void{
			if(event.node){
				onNodeSelect(event);
			}else if(event.edge){
				onEdgeSelect(event);
			}			
		}
		
		private function onNodeSelect(event:SelectionEvent):void{
			var type:String = event.cause.type,
				node:NodeSprite = event.node;
			switch(true){
				case (type == MouseEvent.MOUSE_OVER && !event.ctrlKey ):
					sendNodeRollOverNote( node );
					break;
					
				case (type == MouseEvent.MOUSE_OVER && event.ctrlKey ):
					sendNodeControlRollOverNote( node );
					break;
					
				case (type == MouseEvent.CLICK ):	
					sendNodeClickNote( node );
					break;
					
				
			}
		}
		
		private function onEdgeSelect(event:SelectionEvent):void{
			var type:String = event.cause.type,
				edge:EdgeSprite = event.edge;
			switch(true){
				
				case (type == MouseEvent.CLICK ):	
					sendEdgeClickNote( edge );
					break;
					
				case (type == MouseEvent.MOUSE_OVER ):	
					sendEdgeRollOverNote( edge );
					break;
			}
		}
		
		private function onKeyUp():void{
			if(!view.focusNode)return;
			sendControlKeyUpNote( view.focusNode );
		}
		
		private function onKeyDown():void{
			if(!view.focusNode)return;
			sendControlKeyDownNote( view.focusNode );
		}
		
		private function sendUpdateNote():void{
			//sendNotification(CONTROL_KEY_UP, node );
		}
		
		private function sendControlKeyUpNote(node:NodeSprite):void{
			sendNotification(CONTROL_KEY_UP, node );
		}
		
		private function sendControlKeyDownNote(node:NodeSprite):void{
			sendNotification(CONTROL_KEY_DOWN, node );
		}
		
		private function sendNodeClickNote(node:NodeSprite):void{
			sendNotification(NODE_CLICK, node );
		}
		
		private function sendNodeRollOverNote(node:NodeSprite):void{
			sendNotification(NODE_ROLLOVER, node );
		}
		
		private function sendNodeControlRollOverNote(node:NodeSprite):void{
			sendNotification(NODE_KEY_ROLLOVER, node );
		}
		
		private function sendEdgeClickNote(edge:EdgeSprite):void{
			sendNotification(EDGE_CLICK, edge );
		}
		
		private function sendEdgeRollOverNote(edge:EdgeSprite):void{
			sendNotification(EDGE_ROLLOVER, edge );
		}
		
		
		
		private function sendNodeKeyDeselectNote(node:NodeSprite):void{
			sendNotification(NODE_KEY_ROLLOUT, node );
		}
		
		private function sendNodeDeselectNote(node:NodeSprite):void{
			sendNotification(NODE_ROLLOUT, node );
		}
		
		private function sendEdgeDeselectNote(edge:EdgeSprite):void{
			sendNotification(EDGE_ROLLOUT, edge );
		}
			
		private function get view():FlareViz{
			
			return FlareViz(viewComponent);
		}
		
	}
}